#include <bits/stdc++.h>

using namespace std;

const int maxn=100005;
int dfn[maxn],mx[maxn],seq[maxn],lstdfn,w[maxn],pos[maxn];
int g[maxn],v[maxn*2],nxt[maxn*2];
int num[maxn],n;
int en=2;

inline void addEdge(const int &x,const int &y){
    v[en]=y;nxt[en]=g[x];g[x]=en++;
    v[en]=x;nxt[en]=g[y];g[y]=en++;
}
inline int read(){
    char c;int r;bool f=0;
    for(c=getchar();c>'9'||c<'0';c=getchar()) if(c=='-') f=1;
    for(r=0;c<='9'&&c>='0';c=getchar()) r=r*10+c-'0';
    if(f) return -r;
    else return r;
}

struct S{ int l,r,sum;};
S seg[maxn*20];
int rt[maxn],cseg;

int dest;
inline void inc(int *cur,int old,int l,int r){
    while(l<r){
        *cur=++cseg;
        seg[*cur]=seg[old];
        seg[*cur].sum++;
        int mid=(l+r)>>1;
        if(dest<=mid){
            cur=&seg[*cur].l;
            old=seg[old].l;
            r=mid;
        }else{
            cur=&seg[*cur].r;
            old=seg[old].r;
            l=mid+1;
        }
    }
    *cur=++cseg;
    seg[*cur]=seg[old];
    seg[*cur].sum++;
}
inline int query(int ed,int bg,int l,int r,int k){
    //printf("seg[%d].sum=%d,seg[%d].sum=%d\n",ed,seg[ed].sum,bg,seg[bg].sum);
    while(l<r){
        //printf("l=%d,r=%d\n",l,r);
        int mid=(l+r)>>1;
        if(seg[seg[ed].l].sum-seg[seg[bg].l].sum>=k){
            r=mid;
            ed=seg[ed].l;
            bg=seg[bg].l;
        }else{
            k-=seg[seg[ed].l].sum-seg[seg[bg].l].sum;
            l=mid+1;
            ed=seg[ed].r;
            bg=seg[bg].r;
        }
    }
    return l;
}

void dfs(int p){
    mx[p]=dfn[p]=++lstdfn;
    seq[lstdfn]=w[p];
    dest=w[p];
    inc(rt+lstdfn,rt[lstdfn-1],1,n);
    for(int j=g[p];j;j=nxt[j]){
        const int &tar=v[j];
        if(dfn[tar]) continue;
        dfs(tar);
        mx[p]=max(mx[p],mx[tar]);
    }
}

int main(){
    int m,x,y;

    n=read();
    for(int i=1;i<=n;i++) num[i]=w[i]=read();
    for(int i=1;i<n;i++) addEdge(read(),read());
    sort(num+1,num+1+n);
    for(int i=1;i<=n;i++){
        w[i]=lower_bound(num+1,num+1+n,w[i])-num;
        pos[w[i]]=i;
    }
    dfs(1);
    //for(int i=1;i<=n;i++) dest=w[i],inc(rt+i,rt[i-1],1,n);
    //printf("dfn: ");for(int i=1;i<=n;i++) printf("%d ",dfn[i]); putchar('\n');
    //printf("mx: ");for(int i=1;i<=n;i++) printf("%d ",mx[i]); putchar('\n');

    m=read();
    for(int i=1;i<=m;i++){
        x=read();y=read();
        //printf("querying to rt[%d]=%d from rt[%d-1]=%d for %d:\n",mx[x],rt[mx[x]],dfn[x],rt[dfn[x]-1],mx[x]-dfn[x]+2-y);
        printf("%d\n",pos[query(rt[mx[x]],rt[dfn[x]-1],1,n,y)]);
    }

    return 0;
}

